package com.example.demo.service.impl;

import com.example.demo.pojo.Announcement;
import com.example.demo.pojo.PageBean;
import com.example.demo.pojo.UtilityBill;

import com.example.demo.mapper.UtilityBillMapper;
import com.example.demo.service.UtilityBillService;
import com.example.demo.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UtilityBillServiceImpl implements UtilityBillService {

    @Autowired
    private UtilityBillMapper utilityBillMapper;

    @Override
    public PageBean<UtilityBill> findByuserid(int userid,int pageNum,int pageSize) {
        // 使用PageHelper进行分页设置
        PageHelper.startPage(pageNum, pageSize);
        // 调用Mapper的方法查询数据
        List<UtilityBill> utilityBills = utilityBillMapper.findByUserid(userid);
        // 使用PageInfo包装查询结果，这样便可以获取分页信息
        PageInfo<UtilityBill> pageInfo = new PageInfo<>(utilityBills);
        // 返回自定义的分页结果
        return new PageBean<>(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public void newutilitybill(int userid,double remainingbalance) {
        utilityBillMapper.add(userid,remainingbalance);
    }

    @Override
    public void update(double waterusage, double watercost, double electricityusage, double electricitycost, double totalamount,double remainingbalance,int billid) {

        utilityBillMapper.update(waterusage,watercost,electricityusage,electricitycost,totalamount,remainingbalance,billid);
    }

    @Override
    public UtilityBill selectendnull(int userid) {
        return utilityBillMapper.selectendnull(userid);

    }

    @Override
    public void setendtime(Integer billid) {
        utilityBillMapper.setendtime(billid);
    }

    @Override
    public PageBean<UtilityBill> findByrealname(String realname, int pageNum, int pageSize) {
        // 初始化PageBean对象，用于存放分页后的数据和分页信息
        PageBean<UtilityBill> pb = new PageBean<>();
        // 初始化分页插件，设置当前页码和每页显示的数量
        PageHelper.startPage(pageNum, pageSize);

        // 根据用户ID和类别ID查询公告列表
        List<UtilityBill> as = utilityBillMapper.findByrealname(realname);
        // 将查询结果转换为Page对象，以获取总记录数等分页信息
        Page<UtilityBill> p = (Page<UtilityBill>) as;

        // 将分页信息设置到PageBean中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());

        // 返回包含公告分页信息的PageBean对象
        return pb;
    }

    @Override
    public UtilityBill getBybillid(Integer billid) {
        return utilityBillMapper.getBybillid(billid);
    }
}
